package cc.mrbird.plan.service.impl;

import cc.mrbird.common.domain.QueryRequest;
import cc.mrbird.common.service.impl.BaseService;
import cc.mrbird.plan.dao.CheckTypeMapper;
import cc.mrbird.plan.domain.CheckListType;
import cc.mrbird.plan.service.CheckTypeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service("checkTypeService")
public class CheckTypeServiceImpl extends BaseService<CheckListType> implements CheckTypeService {
    private Logger log = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private CheckTypeMapper checkTypeMapper;
    @Override
    public List<CheckListType> findAllList(CheckListType checkListType, QueryRequest request) {
        try {
            Example example = new Example(CheckListType.class);
            Example.Criteria criteria = example.createCriteria();
            example.setOrderByClause("check_id");
            return this.selectByExample(example);
        } catch (Exception e) {
            log.error("error", e);
            return new ArrayList<>();
        }
    }

    @Override
    public CheckListType findByName(String check_name) {
        Example example = new Example(CheckListType.class);
        example.createCriteria().andCondition("lower(check_name)=", check_name.toLowerCase());
        List<CheckListType> list = this.selectByExample(example);
        return list.isEmpty() ? null : list.get(0);
    }

    @Override
    public void add(CheckListType checkListType) {
        this.save(checkListType);
    }

    @Override
    public void update(CheckListType checkListType) {
        this.updateNotNull(checkListType);
    }

    @Override
    public CheckListType findById(Long id) {
        return this.selectByKey(id);
    }

    @Override
    @Transactional
    public void delete(String ids) {
        List<String> list = Arrays.asList(ids.split(","));
        this.batchDelete(list, "check_id", CheckListType.class);
    }
}
